package com.tyk.bookstore.back.storage.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.storage.manager.excel.constant.ExcelConstant;
import com.tyk.bookstore.back.storage.manager.excel.model.StockExcelVo;
import com.tyk.bookstore.back.storage.model.query.*;
import com.tyk.bookstore.back.storage.model.vo.StockPageVo;
import com.tyk.bookstore.back.storage.model.vo.StockVo;
import com.tyk.bookstore.back.storage.service.StockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author tyk
 * @description 仓库管理
 */
@Tag(name = "仓储管理/库存管理", description = "库存管理")
@RequestMapping({"api/storage/stock"})
@RestController
@RequiredArgsConstructor
public class StockController {
    private final StockService stockService;
    private final ExcelManager<StockExcelVo> excelManager;

    @Operation(summary = "获取库存详情", description = "获取库存详情")
    @SaCheckPermission("storage:stock:get")
    @GetMapping
    public R<StockVo> get(
            @Validated @ParameterObject StockGetQuery query
    ) {
        return R.ok(stockService.get(query.getWarehouseId(), query.getBookId()));
    }

    @Operation(summary = "分页查询库存", description = "分页查询库存")
    @SaCheckPermission("storage:stock:page")
    @GetMapping({"page"})
    public R<StockPageVo> getPage(
            @Validated @ParameterObject StockPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(StockPageVo.class)) : R.ok(stockService.getPage(query));
    }


    @Operation(summary = "添加库存", description = "添加库存")
    @SaCheckPermission("storage:stock:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody StockAddQuery query
    ) {
        stockService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新库存", description = "更新库存")
    @SaCheckPermission("storage:stock:update")
    @PutMapping
    public R<Object> update(
            @Validated @RequestBody StockUpdateQuery query
    ) {
        stockService.update(query);
        return R.ok();
    }

    @Operation(summary = "删除库存", description = "删除库存")
    @SaCheckPermission("storage:stock:delete")
    @DeleteMapping({"single"})
    public R<Object> delete(
            @Validated @ParameterObject StockDeleteQuery query
    ) {
        stockService.delete(query);
        return R.ok();
    }

    @Operation(summary = "批量删除库存", description = "批量删除库存")
    @SaCheckPermission("storage:stock:delete")
    @DeleteMapping({"batch"})
    public R<Object> deleteBatch(
            @Validated List<StockDeleteQuery> queryList
    ) {
        stockService.deleteBatch(queryList);
        return R.ok();
    }


    @DeleteMapping("clean")
    @Operation(summary = "删除数量为0的库存", description = "删除数量为0的库存")
    public R<Object> deleteZero() {
        stockService.deleteZero();
        return R.ok();
    }


    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("storage:stock:excel:download")
    @GetMapping("excel")
    public void downloadExcel(StockPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                stockService.getStockExcelVoList(query),
                ExcelConstant.STOCK_FILE_NAME,
                ExcelConstant.STOCK_SHEET,
                resp
        );
    }

    @Operation(summary = "导入Excel", description = "导入Excel")
    @SaCheckPermission("storage:stock:excel:upload")
    @PostMapping("excel")
    public R<Object> uploadExcel(MultipartFile file) {
        excelManager.save(
                file,
                stockService::saveStockExcelVoList
        );
        return R.ok();
    }

    @Operation(summary = "下载Excel模板", description = "下载Excel模板")
//    @SaCheckPermission("storage:stock:excel:template")
    @GetMapping("excel/template")
    public void excelTemplate(HttpServletResponse resp) {
        excelManager.template(
                ExcelConstant.STOCK_TEMPLATE,
                List.of("仓库ID", "书籍ID", "库存数量"),
                resp
        );
    }

}
